package com.xiaotu.code.util;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.SecureRandom;
import java.util.List;
import java.util.UUID;

import org.apache.commons.codec.digest.DigestUtils;

/**
 * 文件处理工具类.
 * 
 * @author chenxiangrong
 * 
 */
public class FileUtil {
	/**
	 * 获取文件大小.
	 * 
	 * @param filePath
	 * @return
	 */
	public static long getFileSize(String filePath) {
		File file = new File(filePath);
		if (file.exists()) {
			return file.length();
		} else
			return 0l;
	}

	/**
	 * 获取文件后缀名.
	 * 
	 * @param fileName
	 * @return
	 */
	public static String getFileSuffix(String fileName) {
		if (fileName.indexOf(".") > 0) {
			return fileName.substring(fileName.lastIndexOf(".") + 1);
		}
		return null;
	}
	
	public static String getFileName(String filePath){
		int lastIndexOf=filePath.lastIndexOf("/");
		return filePath.substring(lastIndexOf);
	}
	/**
	 * 获取文件的文件夹路径.
	 * @param filePath
	 * @return
	 */
	public static String getFilePath(String filePath){
		filePath=filePath.replace(getFileName(filePath), "");
		return filePath;
	}

	/**
	 * 去除文件名的后缀.
	 * 
	 * @param fileName
	 * @return
	 */
	public static String getFileNameNoSuffix(String fileName) {
		return fileName.substring(0, fileName.lastIndexOf("."));
	}

	/**
	 * 生成文件名(UUID).
	 * 
	 * @param suffix
	 * @return
	 */
	public static String generateFileNameByUUID(String suffix) {
		String newFileName = UUID.randomUUID().toString().replace("-", "");
		return newFileName + "." + suffix;
	}

	/**
	 * 生成文件夹+文件名(文件夹采用文件名md5加密).
	 * 
	 * @param suffix
	 * @return
	 */
	public static String[] generateDirAndNameByUUID(String suffix) {
		String newFileName = UUID.randomUUID().toString().replace("-", "");
		newFileName = newFileName + "." + suffix;
		String secr = DigestUtils.md5Hex(newFileName);
		StringBuilder sb = new StringBuilder("/");
		sb.append(secr.substring(1, 2)).append(secr.substring(3, 4))
				.append("/");
		sb.append(secr.substring(5, 6)).append(secr.substring(7, 8))
				.append("/");
		sb.append(secr.substring(9, 10)).append(secr.substring(11, 12)).append(
				"/");
		String dir = sb.toString();
		String[] dirName = { dir, newFileName };
		return dirName;
	}

	/**
	 * 根据文件名获取文件路径.
	 * 
	 * @param fileName
	 * @return
	 */
	public static String getFileDirByFileName(String fileName) {
		String secr = DigestUtils.md5Hex(fileName);
		StringBuilder sb = new StringBuilder("/");
		sb.append(secr.substring(1, 2)).append(secr.substring(3, 4))
				.append("/");
		sb.append(secr.substring(5, 6)).append(secr.substring(7, 8))
				.append("/");
		sb.append(secr.substring(9, 10)).append(secr.substring(11, 12)).append(
				"/");
		return sb.toString();
	}

	/**
	 * 删除文件.
	 * 
	 * @param filePath
	 */
	public static void deleteFile(String filePath) {
		File file = new File(filePath);
		if (file.exists())
			file.delete();
	}

	/**
	 * 安全删除文件.
	 * 
	 * @param filePath
	 * @throws IOException
	 */
	public static void secureDelete(String filePath) throws IOException {
		File file = new File(filePath);
		if (file.exists()) {
			SecureRandom random = new SecureRandom();
			RandomAccessFile raf = new RandomAccessFile(file, "rw");
			FileChannel channel = raf.getChannel();
			MappedByteBuffer buffer = channel.map(
					FileChannel.MapMode.READ_WRITE, 0, raf.length());
			// overwrite with zeros
			while (buffer.hasRemaining()) {
				buffer.put((byte) 0);
			}
			buffer.force();
			buffer.rewind();
			// overwrite with ones
			while (buffer.hasRemaining()) {
				buffer.put((byte) 0xFF);
			}
			buffer.force();
			buffer.rewind();
			// overwrite with random data; one byte at a time
			byte[] data = new byte[1];
			while (buffer.hasRemaining()) {
				random.nextBytes(data);
				buffer.put(data[0]);
			}
			buffer.force();
			file.delete();
		}
	}

	/**
	 * 批量删除.
	 * 
	 * @param fileList
	 */
	public static void deleteMoreFromDir(List<File> fileList) {
		for (File file : fileList) {
			if (file.exists())
				file.delete();
		}
	}

	/**
	 * 文件类型过滤(true:符合；false:不符合).
	 * 
	 * @param fileName
	 * @return
	 */
	public static boolean fileTypeFilter(String fileName, String allowType) {
		boolean flag = false;
		String suffix = getFileSuffix(fileName);
		if (allowType.lastIndexOf(suffix) > -1) {
			flag = true;
		}
		return flag;
	}

	/**
	 * 过滤文件大小(true:符合；false:不符合).
	 * 
	 * @param filePath
	 * @param limitSize
	 * @return
	 */
	public static boolean fileSizeFilter(String filePath, long limitSize) {
		boolean flag = false;
		if (limitSize >= getFileSize(filePath)) {
			flag = true;
		}
		return flag;
	}
}
